from flask import render_template, Blueprint, abort
from flask import g, request, flash, current_app, redirect, url_for

from app import app 
from models import User, Post, Tag 
from forms import SignupForm, LoginForm, PostForm

from utils import current_user, login_user, logout_user

@app.before_request
def before_request():
    g.user = current_user()

@app.route('/')
def index():
    return render_template('index.html', title='index')

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    error = None 
    form = SignupForm(request.form) 
    if request.method == 'POST':
        if form.validate_on_submit():
            print request.form
            user = User(request.form)
            user.save()
            login_user(user)
            return redirect(url_for('index'))
        else:
            error = 'validation failed' 
            print form.errors
             
    return render_template('signup.html', title='signup', form=form)

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None 
    form = LoginForm(request.form) 
    if request.method == 'POST':
        if form.validate_on_submit():
            user = User.query.filter_by(username=request.form['username']).first()  
            if user is None:
                error = 'username invalidate' 
            else:
                if user.password == request.form['password']:
                    login_user(user)
                    return redirect(url_for('index'))
                else:
                    error = 'password invalidate' 
        else:
            error = 'validate failed' 
            print form.errors
             
    return render_template('login.html', title='login', form=form)

@app.route('/logout', methods=['GET', 'POST'])
def logout():
    error = None 
    if request.method == 'POST':
        logout_user(g.user)
        g.user = None
    else:
        logout_user(g.user)
        g.user = None
        error = 'validation failed' 
    return redirect(url_for('index'))

@app.route('/posts/', methods=['GET'])
def posts():
    posts = Post.query.all()
    return render_template('posts.html', title='posts', posts=posts)

@app.route('/posts/new/', methods=['GET'])
def posts_new():
    form = PostForm()
    return render_template('posts_new.html', title='posts', form=form)

@app.route('/posts/create/', methods=['POST'])
def posts_create():
    user = current_user()
    if user is None:
        error = 'no login'
    else:
        post = Post()
        post.title = request.form['title']
        post.content = request.form['content']
        post.user_id = user.id
        post.tag_id = 1 
        post.save()

    return redirect(url_for('posts'))

@app.route('/posts/<post_id>', methods=['GET'])
def posts_show(post_id):
    post = Post.query.filter_by(id = post_id).first() 
    if post is None:
        error = 'invalidate post'
        return redirect(url_for('posts'))
    else:
        return render_template('posts_show.html', title='post show', post=post)

@app.route('/posts/edit/<post_id>', methods=['GET'])
def posts_edit(post_id):
    post = Post.query.filter_by(id = post_id).first() 
    if post is None:
        error = 'invalidate post'
        return redirect(url_for('posts'))
    else:
        form = PostForm()
        form.title.data = post.title
        form.content.data = post.content
        return render_template('posts_edit.html', title='post edit', form=form, post_id=post_id)

@app.route('/posts/update/<post_id>', methods=['POST'])
def posts_update(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if post is None:
        return redirect(url_for('posts'))
    else:
        post.title = request.form['title']
        post.content = request.form['content']
        post.user_id = g.user.id
        post.tag_id = 1 
        post.save()
    return redirect(url_for('posts'))

@app.route('/posts/delete/<post_id>', methods=['GET', 'DELETE'])
def posts_delete(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if not post is None:
        post.delete()
    return redirect(url_for('posts'))
